package xc.com.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import xc.com.common.Result;
import xc.com.common.StatusCode;
import xc.com.course.domain.TeachplanMedia;
import xc.com.course.feign.OssFegin;
import xc.com.course.mapper.TeachplanMediaMapper;
import xc.com.course.service.TeachplanMediaService;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhuolizhong
 * @since 2021-06-03
 */
@Service
public class TeachplanMediaServiceImpl extends ServiceImpl<TeachplanMediaMapper, TeachplanMedia> implements TeachplanMediaService {
    @Autowired
    private OssFegin ossFegin;
    @Autowired
    private TeachplanMediaMapper teachplanMediaMapper;

    @Override
    public Result addTeachPlanMedia(String courseId,String teachplanId,String mediaName,MultipartFile file) {
        if (file != null) {
            String upload = ossFegin.upload(file);
            if(StringUtils.isBlank(upload)){
                return   new Result(false, StatusCode.FAIL, "上传视频失败！");
            }
            TeachplanMedia teachplanMedia=new TeachplanMedia();
            teachplanMedia.setTeachplanId(teachplanId);
            teachplanMedia.setMediaFileoriginalname(mediaName);
            teachplanMedia.setMediaUrl(upload);
            teachplanMedia.setCourseid(courseId);
            int i = teachplanMediaMapper.insert(teachplanMedia);
            if(i>0){
                return new Result(true, StatusCode.OK, "添加课程计划视频成功！");
            }
            return new Result(false, StatusCode.FAIL, "添加课程计划失败！");
        }
      return   new Result(false, StatusCode.FAIL, "没有获取到上传视频资源！");
    }


    @Override
    public Result updateTeachPlanMedia(String courseI,String teachplanId,String mediaId,MultipartFile file) {
        if(file!=null){
            TeachplanMedia teachplanMedia = teachplanMediaMapper.selectById(mediaId);
            if(teachplanMedia!=null){

            }
        }
        return   new Result(false, StatusCode.FAIL, "没有获取到上传视频资源！");
    }

    @Override
    public TeachplanMedia findByTeachplanId(String teachplanId) {
        QueryWrapper<TeachplanMedia> wrapper=new QueryWrapper<>();
        wrapper.eq("teachplan_id",teachplanId);
        TeachplanMedia media = teachplanMediaMapper.selectOne(wrapper);
        TeachplanMedia teachplanMedia = teachplanMediaMapper.selectById(teachplanId);
        return media;
    }

    @Override
    public Result addMedia(TeachplanMedia teachplanMedia) {
        System.out.println(teachplanMedia);
        if(teachplanMedia==null){
            return   new Result(false, StatusCode.FAIL, "没有课程计划视频信息！");
        }
        int i;
        if(teachplanMedia.getMediaId()!=null){
             i = teachplanMediaMapper.updateById(teachplanMedia);
            System.out.println("修改了");
        }else {
             i = teachplanMediaMapper.insert(teachplanMedia);
            System.out.println("添加了");
        }
        if(i<=0){
            return   new Result(false, StatusCode.FAIL, "添加失败！");
        }
        return   new Result(true, StatusCode.FAIL, "添加成功！");
    }
}
